import os
import pandas as pd
from utils import df_into_db, read_sql


def get_cm_symbols():
    """
    得到币本位合约
    :return:
    """
    cm_path = "E:/data/funding_rate/all_cm_funding_rate.csv"
    cm_df = pd.read_csv(cm_path)
    cm_symbols = cm_df.columns.tolist()
    cm_symbols.remove("datetime")
    return cm_symbols


def get_um_symbols():
    """
    得到U本位合约
    :return:
    """
    um_path = "E:/data/funding_rate/all_um_funding_rate.csv"
    um_df = pd.read_csv(um_path)
    um_symbols = um_df.columns.tolist()
    um_symbols.remove("datetime")
    return um_symbols


def get_exist_symbols():
    df = read_sql("select distinct symbol from all_market_funding_rate", db_name="dataloader")
    return set(df.symbol)


def main():
    cm_symbols = get_cm_symbols()
    cm_symbols.append("EOSUSD_PERP")
    um_symbols = get_um_symbols()
    um_symbols.append("EOSUSDT")
    path = "E:/data/funding_rate/all_market_funding_rate"
    files = os.listdir(path)
    exist_symbols = get_exist_symbols()
    for file in files:
        # print(file)
        symbol = file.replace("_funding_rate.csv", "")  # 注意: 有些合约的名字含有下划线
        if symbol in exist_symbols:
            continue
        df = pd.read_csv(os.path.join(path, file))
        assert symbol == df.iloc[0]["symbol"], "文件和内容必须一致"
        if symbol in cm_symbols:
            type = "cm"
        elif symbol in um_symbols:
            type = "um"
        else:
            raise Exception("type must be cm or um!")
        df["type"] = type
        df.drop(["time", "date"], axis=1, inplace=True)  # drop掉time和date列 datetime信息完备
        df_into_db(df, "dataloader", "all_market_funding_rate")


if __name__ == "__main__":
    main()